package com.skyc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.skyc.dao.collectionDao;
import com.skyc.entity.Clothes;
import com.skyc.entity.User;

import com.skyc.util.JDBCUtil;

public class collectionDaoImpl implements collectionDao{
	JDBCUtil util = JDBCUtil.getJDBCUtil();
	@Override
	public boolean addcollection(User user, int ccid) {
		String sql = "INSERT into t_favorites(u_id,c_cid) values (?,?)";
		return util.update(sql, user.getUid(), ccid);
	}
	@Override
	public boolean deletecollection(User user, int ccid) {
		String sql = "delete from t_favorites where u_id=? and c_cid=?";
		return util.update(sql, user.getUid(), ccid);
	}
	public List<Clothes> ucollection(User user) {
		List<Clothes> list = new ArrayList<Clothes>();
		String sql = "select c_cid from t_favorites where u_id="+user.getUid();
		System.out.println(sql);
		CachedRowSet rs = util.query(sql);
		try {
			while (rs.next()) {
				String sql1 = "SELECT * FROM t_clothes WHERE c_cid = ? limit 1";
				CachedRowSet rs1 = util.query(sql1, rs.getInt("c_cid")+"");
				if(rs1.next()) {
					Clothes c = new Clothes();
					c.setCcolor(rs1.getString("c_color"));
					c.setCcid(rs1.getInt("c_cid"));
					c.setCid(rs.getInt("c_cid"));
					c.setCimgurl(rs1.getString("c_imgurl"));
					c.setCmaintype(rs1.getString("c_maintype"));
					c.setCname(rs1.getString("c_name"));
					c.setCprice(rs1.getInt("c_price"));
					c.setCsales(rs1.getInt("c_sales"));
					c.setCsex(rs1.getString("c_sex"));
					c.setCsize(rs1.getString("c_size"));
					c.setCstock(rs1.getInt("c_stock"));
					c.setCtype(rs1.getString("c_type"));
					list.add(c);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeCacheRowSet(rs);
		}
		return list;

	}
	

}
